// Fill out your copyright notice in the Description page of Project Settings.


#include "AbilitySystem/Abilities/AuraSummonAbility.h"

#include "Kismet/KismetSystemLibrary.h"


TArray<FVector> UAuraSummonAbility::GetSpawnLocations()
{
	// 获取召唤师的朝向向量
	const FVector ActorForwardVector = GetAvatarActorFromActorInfo()->GetActorForwardVector();
	//获取召唤师的位置
	const FVector ActorLocation = GetAvatarActorFromActorInfo()->GetActorLocation();
	//将召唤的角度范围进行分段，在每段里面生成一个召唤物
	float DeltaSpread = SpawnSpread / (NumMinions - 1);
	//获取到最左侧的角度
	const FVector LeftOfSpread = ActorForwardVector.RotateAngleAxis(-SpawnSpread / 2, FVector::UpVector);

	TArray<FVector> SpawnLocations;
	for (int32 i = 0; i < NumMinions; i++) //遍历，在每个分段上面获取位置
	{
		//获取当前分段的角度
		FVector Direction = LeftOfSpread.RotateAngleAxis(DeltaSpread * i, FVector::UpVector);
		//随机位置，加上原始位置就是偏移的位置
		FVector ChosenSpawnLocation = ActorLocation + Direction * FMath::RandRange(MinSpawnDistance, MaxSpawnDistance);

		FHitResult Hit;
		// 添加上射线拾取，防止生成的角色卡住。
		GetWorld()->LineTraceSingleByChannel(
			Hit,
			ChosenSpawnLocation + FVector(0.f, 0.f, 400.f),
			ChosenSpawnLocation - FVector(0.f, 0.f, 400.f),
			ECC_Visibility
		);
		if (Hit.bBlockingHit)
		{
			ChosenSpawnLocation = Hit.ImpactPoint;
		}
		SpawnLocations.Add(ChosenSpawnLocation);
	}
	
	return SpawnLocations;
}

TSubclassOf<APawn> UAuraSummonAbility::GetRandomMinionClass()
{
	return MinionClasses[FMath::RandRange(0, MinionClasses.Num() - 1)];
}
